<%@ page import="cn.edu.usst.cs.captcha01.HelloServlet" %>
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>

<!DOCTYPE html>
<html>
<head>
    <title>CAPTCHA test01</title>
</head>
<body>
<%
    int ID;
    try {
        String ID_raw = request.getSession().getAttribute(HelloServlet.SITE_ID_ATTR).toString();
        ID = Integer.parseInt(ID_raw);
    } catch (RuntimeException ignored) { %>
<p>未知访问标识</p>
</body>
</html>
<%
        return;
    }
%>
<h1><%= "登陆界面" %>
</h1>
<br/>
<form action="result" method="post">
    <div id="captcha_area">
        <%
            SHOW_IMG:
            {
                Object length_raw = request.getSession().getAttribute(HelloServlet.CAPTCHA_LENGTH_ATTR);
                if (length_raw == null) {
        %>
        <p>验证码要求长度解析错误</p>
        <%
                break SHOW_IMG;
            }
            int LENGTH;
            try {
                LENGTH = Integer.parseInt(length_raw.toString());
            } catch (NumberFormatException exception) {
        %>
        <p>验证码要求长度解析错误</p>
        <%
                break SHOW_IMG;
            }
            for (int i = 0; i < LENGTH; i++) {
                char dig = String.valueOf(i).charAt(0);
                String img_path_attr = HelloServlet.getCaptchaPicAttr(dig);
                Object img_path_raw = request.getSession().getAttribute(img_path_attr);
                String img_err_msg = String.format("图像%s解析错误", img_path_attr);
                if (img_path_raw == null) {
        %>
        <p><%=img_err_msg%>
        </p>
        <%
                continue;
            }
        %>
        <img src="<%= img_path_raw.toString() %>" alt="<%= img_err_msg %>-无效路径"/>
        <%

                }
            }
        %>
    </div>

    <fieldset>
        <label for="<%=HelloServlet.CAPTCHA_ATTR%>">请输入上方验证码
            <input name="<%=HelloServlet.CAPTCHA_ATTR%>" id="<%=HelloServlet.CAPTCHA_ATTR%>">
        </label>
        <button type="submit" value="<%=ID%>">提交</button>
        <p>访问编号</p>
        <label for="<%=HelloServlet.SITE_ID_ATTR%>">
            <input name="<%=HelloServlet.SITE_ID_ATTR%>" id="<%=HelloServlet.SITE_ID_ATTR%>" value="<%=ID%>" readonly>
        </label>
    </fieldset>
</form>

</body>
</html>